import { onMounted, onUnmounted } from 'vue'

// 监听浏览器刷新
export const useBeforeunload = (fn: () => void) => {
  const beforeunloadHandle = (e: Event) => {
    fn && fn()
  }

  onMounted(() => {
    // 添加浏览器刷新监听
    window.addEventListener('beforeunload', beforeunloadHandle)
  })

  onUnmounted(() => {
    // 移除浏览器刷新监听
    window.removeEventListener('beforeunload', beforeunloadHandle)
  })
}
